/**
 * @param {number[][]} mat
 * @param {number} target
 * @return {number}
 */
 var minimizeTheDifference = function(mat, target) {
  const m = mat.length, n = mat[0].length
  const dp = new Array(m).fill(0).map(() => new Array(n))
  for(let i = 0; i<n; i++){
    dp[0][i] = [mat[0][i]]
  }
  let sumArr = [...mat[0]]
  const getNow = (t) => { 
    return sumArr.map(item => item+t)
  }
  for(let i = 1; i<m; i++){
    for(let j = 0; j<n; j++){
      dp[i][j] = getNow(mat[i][j])
    }
    sumArr = []
    for(let j = 0; j<n; j++){
      sumArr = [...new Set(sumArr.concat(dp[i][j]))]
    }
  }
  const last = sumArr
  let ans = 0, pre = Number.MAX_SAFE_INTEGER
  for(const x of last){
    const dif = Math.abs(x - target)
    if(dif < pre){
      ans = dif
      pre = dif
    }
  }
  return ans
};